/**
 * $Id: SMGP13SubmitMessage.java,v 1.2 2006/09/12 03:03:01 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.smgpmsg;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public class SMGP13SubmitMessage extends SMGPMessage {

    private StringBuffer strBuf;

    public SMGP13SubmitMessage(int subType, int needReport, int priority,
            String serviceId, String feeType, String feeCode, int msgFormat,
            Date validTime, Date atTime, String srcTermId, String chargeTermId,
            String destTermId[], String msgContent, String reserve)
            throws IllegalArgumentException {
        if (subType < 0 || subType > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MsgType less than 0 or larger than 255.");
        if (needReport < 0 || needReport > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:NeedReport less than 0 or larger than 255.");
        if (priority < 0 || priority > 9)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:Priority less than 0 or larger than 9.");

        if (serviceId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:ServiceID value is null.");

        if (serviceId.length() > 10)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:ServiceID length is larger than 10");

        if (feeType == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:FeeType value is null.");

        if (feeType.length() > 2)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:FeeType length is larger than 2");

        if (feeCode == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:FeeCode value is null.");

        if (feeCode.length() > 6)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:FeeCode length is larger than 6");

        if (msgFormat < 0 || msgFormat > 255)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MsgFormat less than 0 or larger than 255.");

        if (srcTermId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:SrcTermID value is null.");

        if (srcTermId.length() > 21)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:SrcTermID length is larger than 21");

        if (chargeTermId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:ChargeTermID value is null.");

        if (chargeTermId.length() > 21)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:ChargeTermID length is larger than 21");

        if (destTermId == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:DestTermID value is null.");

        if (destTermId.length > 100)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:DestTermId length is larger than 100");

        for (int i = 0; i < destTermId.length; i++)
            if (destTermId[i].length() > 21)
                throw new IllegalArgumentException(
                        "The parameter of submit message is wrong:one DestTermID length is larger than 21");

        if (msgContent == null)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MsgContent value is null.");

        if (msgContent.length() > 252)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:MsgContent length is larger than 252");

        if (reserve != null && reserve.length() > 8)
            throw new IllegalArgumentException(
                    "The parameter of submit message is wrong:reserve length is larger than 8");

        int len = 120 + 21 * destTermId.length + msgContent.length();
        buf = new byte[len];
        TypeConvert.int2byte(len, buf, 0);
        TypeConvert.int2byte(SMGPCommon.SMGP_SUBMIT, buf, 4);
        buf[12] = (byte) subType;
        buf[13] = (byte) needReport;
        buf[14] = (byte) priority;
        System.arraycopy(serviceId.getBytes(), 0, buf, 15, serviceId.length());
        System.arraycopy(feeType.getBytes(), 0, buf, 25, feeType.length());
        System.arraycopy(feeCode.getBytes(), 0, buf, 27, feeCode.length());
        buf[33] = (byte) msgFormat;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        if (validTime != null) {
            String tmpTime = String.valueOf(
                    String.valueOf(dateFormat.format(validTime)))
                    .concat("032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, 34, 16);
        }
        if (atTime != null) {
            String tmpTime = String.valueOf(
                    String.valueOf(dateFormat.format(atTime))).concat("032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, 51, 16);
        }
        System.arraycopy(srcTermId.getBytes(), 0, buf, 68, srcTermId.length());
        System.arraycopy(chargeTermId.getBytes(), 0, buf, 89, chargeTermId
                .length());
        buf[110] = (byte) destTermId.length;
        int i = 0;
        for (i = 0; i < destTermId.length; i++)
            System.arraycopy(destTermId[i].getBytes(), 0, buf, 111 + i * 21,
                    destTermId[i].length());

        int loc = 111 + i * 21;
        buf[loc] = (byte) msgContent.length();
        System.arraycopy(msgContent.getBytes(), 0, buf, loc + 1, msgContent
                .length());
        loc = loc + 1 + msgContent.length();
        if (reserve != null)
            System.arraycopy(reserve.getBytes(), 0, buf, loc, reserve.length());
        strBuf = new StringBuffer();
        strBuf.append(",subType=").append(subType);
        strBuf.append(",NeedReport=").append(needReport);
        strBuf.append(",Priority=").append(priority);
        strBuf.append(",ServiceID=").append(serviceId);
        strBuf.append(",FeeType=").append(feeType);
        strBuf.append(",FeeCode=").append(feeCode);
        strBuf.append(",MsgFormat=").append(msgFormat);
        if (validTime != null)
            strBuf.append(",ValidTime=").append(dateFormat.format(validTime));
        else
            strBuf.append(",ValidTime=null");
        if (atTime != null)
            strBuf.append(",AtTime=").append(dateFormat.format(atTime));
        else
            strBuf.append(",at_Time=null");
        strBuf.append(",SrcTermID=").append(srcTermId);
        strBuf.append(",ChargeTermID=").append(chargeTermId);
        strBuf.append(",DestTermIDCount=").append(destTermId.length);
        for (int t = 0; t < destTermId.length; t++)
            strBuf.append(",DestTermID[").append(t).append("]=").append(
                    destTermId[t]);

        strBuf.append(",MsgLength=").append(msgContent.length());
        strBuf.append(",MsgContent=").append(msgContent);
        strBuf.append(",Reserve=").append(reserve);
    }

    public String toString() {
        StringBuffer outBuf = new StringBuffer();
        outBuf.append("SMGPSubmitMessage: ");
        outBuf.append("PacketLength=").append(buf.length);
        outBuf.append(",RequestID=0x").append(Integer.toHexString(getRequestId()));
        outBuf.append(",SequenceID=").append(getSequenceId());
        if (strBuf != null)
            outBuf.append(strBuf.toString());
        return outBuf.toString();
    }

    public int getRequestId() {
        return SMGPCommon.SMGP_SUBMIT;
    }
}
